
$('#mqtt-msg-chat').css('height',(window.innerHeight-100)+'px');



function chat() {
    var chatdata = $("#chat-mainform").serializeObject();
    if( !chatdata.chattext){
        pop_up("输入聊天内容");
        return;
    }
    if( !mqttmsg.fromId ){
        pop_up("未找到对话人！");
        return ;
    }

    var chatinfo = {};
    chatinfo.nickname = mqttmsg.nickname;
    chatinfo.image=mqttmsg.headimgurl;
    chatinfo.date=mqttmsg.getDate();
    chatinfo.stime = mqttmsg.getTime();
    chatinfo.content = {txt:chatdata.chattext} ;
    chatinfo.memberId = mqttmsg.memberId;
    chatinfo.groupName=mqttmsg.getGroupName();
    ///console.log(mqttmsg.getGroup())
    mqttmsg.getGroup().chats.push({content:chatinfo.content,memberId:mqttmsg.memberId,stime: mqttmsg.getTime()});
    mqttmsg.getGroup().nickname=mqttmsg.nickname;
    mqttmsg.getGroup().image=mqttmsg.headimgurl;
    mqttmsg.getGroup().date=mqttmsg.getDate();

    mqttmsg.group.nickname=mqttmsg.nickname;
    mqttmsg.group.image=mqttmsg.headimgurl;
    mqttmsg.group.date=mqttmsg.getDate();
    mqttmsg.group.chats.push({content:chatinfo.content,memberId:mqttmsg.memberId,stime:mqttmsg.getFormatTime(),date:mqttmsg.getDate(),image:mqttmsg.headimgurl});

    mqttmsg.client.publish(mqttmsg.gettopicName() , JSON.stringify(chatinfo), 1, function(error) {
        console.log(error || '消息发布成功');
        //重写html
      rewrite(true);
    });
    let orderId = server.getQueryString("orderId");
    mqttmsg.client.publish(mqttmsg.serverTopic , JSON.stringify({code:0,data:chatinfo,chats:{content:chatinfo.content,memberId:mqttmsg.memberId,stime: mqttmsg.getTime()},orderId:orderId}), 1, function(error) {
        console.log(error || '服务器消息发布成功');
    })
   // console.log( mqttmsg.mqtt_content);
    server.setchatnews(mqttmsg.mqtt_key,JSON.stringify( mqttmsg.mqtt_content));
     $("#chattext").val("");
};
//t是否把光标移到最底部

function rewrite  (t)   {
    var hash=window.location.hash.substr(2);
    if(!mqttmsg.fromId||hash!="page/chat/chat")return;
    mqttmsg.loadNewChats(mqttmsg.fromId);
   // console.log(mqttmsg.group)
    server.loadData("#mqtt-msg-chat", mqttmsg.group);
    if(t){
       var ele =  document.getElementById('mqtt-msg-chat');
       if(ele&&ele.scrollHeight)
       ele.scrollTop = ele.scrollHeight ;
    }
}

window.addEventListener('mqttnewchatsNotie', (event)=>{
//  console.log(event.detail);
    rewrite(event.detail);
})

function queryolddata(){
    mqttmsg.loadoldchats1(mqttmsg.fromId);
    rewrite();
}

function chat2(e) {
    var evt = window.event || e;
    if (evt.keyCode == 13) {
       chat();
       // return;
    }
};


function getconfig(res) {
    var config = res.data;
    //alert("config:" + JSON.stringify(config));
    wx.config({
        //	debug: true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
        appId: config.appId, // 必填，公众号的唯一标识
        timestamp: config.timestamp, // 必填，生成签名的时间戳
        nonceStr: config.nonceStr, // 必填，生成签名的随机串
        signature: config.signature, // 必填，签名
        jsApiList: [ "checkJsApi",
            "startRecord", //开始录音接口
            "stopRecord", // 停止录音接口
            "uploadVoice", //上传语音接口
              ],
        success: function (res) {// 以键值对的形式返回，可用true，不可用false。如：{"checkResult":{"scanQRCode":true},"errMsg":"checkJsApi:ok"}
            //	alert("success:" + JSON.stringify(res));
            if (res.checkResult.scanQRCode != true) {
                alert('抱歉，当前客户端版本不支');
                return;
            }
        },
        fail: function (res) { //检测getNetworkType该功能失败时处理
            alert('checkJsApi error');

        }
    });

    wx.checkJsApi({
        jsApiList: [
            "startRecord",
            "stopRecord",
            "uploadVoice",
            "onVoiceRecordEnd"
        ],
        fail: res => {
            this.$toast.fail("您的微信版本太低，请使用最新的微信客户端");
        }
    });
    wx.ready(function() {
        // config信息验证后会执行ready方法，所有接口调用都必须在config接口获得结果之后，config是一个客户端的异步操作，所以如果需要在页面加载时就调用相关接口，则须把相关接口放在ready函数中调用来确保正确执行。对于用户触发时才调用的接口，则可以直接调用，不需要放在ready函数中。
      console.log("成功加载录音功能！")
    });

}


 function init() {

    mqttmsg.fromId = server.getQueryString("teacherTopicID");
    if( !mqttmsg.fromId||mqttmsg.fromId=='undefined' ){
        pop_up("未获取对话用户！");
        server.loadindex('page/home/home');
        return;
    }
  //  console.log('fromTopic='+"xuanzhu_"+   mqttmsg.fromId);
    server.getUserInfo(function (res) {
        mqttmsg.memberId = res.id;
        if( mqttmsg.memberId== mqttmsg.fromId){
            pop_up_pos("自己咨询自己，疯了？",null,"200px")
            server.loadindex('page/home/home');
        }
        mqttmsg.headimgurl = res.headimgurl;
        mqttmsg.nickname = res.nickname;
    });
    mqttmsg.initMQTT( mqttmsg.fromId);

  rewrite(true) ;

     $("#scanner").click(function (){
         var url =(server.isIos()&&server.iosurl)?server.iosurl: window.location.href.split('#')[0];
         $.get("wechat/scanConfig", {
             "pageName" : url
         }, function(res) {
             getconfig(res);
         })
     })


}


function touchend(){
    console.log("结束")
    alert("松开");
}

function touchstart(){
    console.log("按下")
}

function handleTouchMove(e){
    console.log(e);
}

init();